<template>
	<div class="wrapper">
		<!--header部分-->
		<header>
			<div class="back" @click="toWallet">
			<i class="fa fa-chevron-left"></i>
			</div>
			<div class="text">提现</div>
		</header>
		<!--提现金额部分-->
		<ul class="form-box">
			<li>
				<div class="title">
					提现金额：
				</div>
				<div class="withdraw">
					<input type="text" v-model="amount" placeholder="提现金额">
				</div>
			</li>
		</ul>
		<div class="button-tx">
			<button @click="tx">提现</button>
		</div>
	</div>
</template>

<script>
	export default {
		name: 'Tx',
		data() {
			return {
				amount:'',
				user: {},
				updateuser:{},
			}
		},
		created(){
			this.user = this.$getSessionStorage('user');
			this.$axios.post('UserCartController/getUserCartByUserId',this.$qs.stringify({
				userId:this.user.userId,
			})).then(response=>{
				this.usercart=response.data;
			}).catch(error => {
				console.error(error);
			});
			this.$axios.post('UserController/getUserByUserId',this.$qs.stringify({
				userId:this.user.userId,
			})).then(response=>{
				this.updateuser=response.data;
			}).catch(error => {
				console.error(error);
			});
		},
		methods: {
			tx(){
				if(this.updateuser.walletBalance-this.amount<0){
					alert('余额不足！');
				}
				else{
					this.$axios.post('UserCartController/subBalanceById',this.$qs.stringify({
						userId:this.user.userId,
						amount:this.amount,
						balance:Number(this.usercart.balance)+Number(this.amount),
					})).then(response=>{
						
					}).catch(error=>{
						console.error(error);
					});
					
					this.$axios.post('TranDetailController/saveTranDetail',this.$qs.stringify({
						userId:this.user.userId,
						amount:this.amount,
						trantype:'2',
					})).then(response=>{
						
					}).catch(error=>{
						console.error(error);
					});
					
					this.$axios.post('UserController/addBalanceById',this.$qs.stringify({
						userId:this.user.userId,
						amount:this.amount,
						walletBalance:Number(this.updateuser.walletBalance)-Number(this.amount),
					})).then(response=>{
						alert('提现成功！');
					}).catch(error=>{
						console.error(error);
					});
				}
			},
			
			toWallet() {
				this.$router.push({
					path: '/wallet'
				});
			},
			
			
		}
	}
</script>

<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		width: 100%;
		height: 100%;
		background-color: azure;
	}
	/****************** header ******************/
	.wrapper header{
		width: 100%;
		height: 12vw;
		background-color: #FFF;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		margin-bottom: 1vw;
	}
	.wrapper header .back{
		font-size: 4vw;
		margin-left: 2vw;
		margin-right: 2vw;
	}
	.wrapper header .text{
		font-size: 4vw;
	}
	
	.wrapper .form-box {
		width: 100%;
		
	}
	
	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
		display: flex;
		align-items: center;
	}
	
	.wrapper .form-box li .title {
		flex: 0 0 18vw;
		font-size: 3vw;
		font-weight: 700;
		color: #666;
	}
	
	.wrapper .form-box li .withdraw {
		flex: 1;
	}
	
	.wrapper .form-box li .withdraw input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 3vw;
	}
	
	.wrapper .button-tx {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}
	
	.wrapper .button-tx button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #fff;
		background-color: #38CA73;
		border-radius: 4px;
		border: none;
		outline: none;
	}
</style>
